package com.zking.dto;

import com.zking.entity.Order;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

// 和前端进行数据传输的载体 {id:xxx, username:xxx, userId:xxx, info:xxx...}
@Data @AllArgsConstructor @NoArgsConstructor
public class OrderDto {
    private Integer orderId;
    private Integer userId;
    private String username;
    private String orderInfo;
    private String address;
    private Double price;
    private Double minPrice;
    private Double maxPrice;
    
    // DTO可以和实体类互转
    public static Order toOrder(OrderDto orderDto) {
        Order order = new Order();
        order.setId(orderDto.getOrderId());
        order.setUserId(orderDto.getUserId());
        order.setInfo(orderDto.getOrderInfo());
        order.setAddress(orderDto.getAddress());
        order.setPrice(orderDto.getPrice());
        return order;
    }
    
    public static OrderDto fromOrder(Order order) {
        OrderDto orderDto = new OrderDto();
        orderDto.setOrderId(order.getId());
        orderDto.setUserId(order.getUserId());
        // orderDto.setUsername(order.getUserId());
        orderDto.setOrderInfo(order.getInfo());
        orderDto.setAddress(order.getAddress());
        orderDto.setPrice(order.getPrice());
        return orderDto;
    }
}
